package com.example.demo.service.impl;

import com.example.demo.entity.Interview;
import com.example.demo.entity.InterviewQuestion;
import com.example.demo.mapper.InterviewMapper;
import com.example.demo.mapper.InterviewQuestionMapper;
import com.example.demo.service.InterviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

@Service
public class InterviewServiceImpl implements InterviewService {

    @Autowired
    private InterviewMapper interviewMapper;

    @Autowired
    private InterviewQuestionMapper interviewQuestionMapper;

    @Override
    public boolean save(Interview interview) {
        interview.setStatus(0); // 初始状态为未开始
        interview.setCreateTime(LocalDateTime.now());
        interview.setUpdateTime(LocalDateTime.now());
        return interviewMapper.insert(interview) > 0;
    }

    @Override
    public boolean update(Interview interview) {
        interview.setUpdateTime(LocalDateTime.now());
        return interviewMapper.update(interview) > 0;
    }

    @Override
    public Interview findById(Long id) {
        return interviewMapper.findById(id);
    }

    @Override
    public List<Interview> findByUserId(Long userId) {
        return interviewMapper.findByUserId(userId);
    }

    @Override
    public boolean delete(Long id) {
        // 先删除相关的面试问题
        interviewQuestionMapper.deleteByInterviewId(id);
        return interviewMapper.delete(id) > 0;
    }

    @Override
    public boolean saveQuestion(InterviewQuestion question) {
        question.setCreateTime(LocalDateTime.now());
        question.setUpdateTime(LocalDateTime.now());
        return interviewQuestionMapper.insert(question) > 0;
    }

    @Override
    public List<InterviewQuestion> findQuestionsByInterviewId(Long interviewId) {
        return interviewQuestionMapper.findByInterviewId(interviewId);
    }
}